﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using HtmlAgilityPack; 
using System.Windows.Forms; 

namespace CrawlerApp
{
    public class Crawler
    {
        private Form1 form;
        private HttpClient httpClient;
        private HashSet<string> visitedUrls = new HashSet<string>();

        public Crawler(Form1 form)
        {
            this.form = form;
            httpClient = new HttpClient();
        }

        public async void InitCrawl(string initialUrl)
        {
            await CrawlPage(initialUrl);
        }

        private async Task CrawlPage(string url)
        {
            if (!visitedUrls.Contains(url))
            {
                visitedUrls.Add(url);
                try
                {
                    HttpResponseMessage response = await httpClient.GetAsync(url);
                    if (response.IsSuccessStatusCode && response.Content.Headers.ContentType.MediaType == "text/html")
                    {
                        string content = await response.Content.ReadAsStringAsync();
                        form.Invoke(new Action(() => form.ListUrls.Items.Add(url)));

                        var htmlDoc = new HtmlAgilityPack.HtmlDocument();
                        htmlDoc.LoadHtml(content);

                        foreach (var link in htmlDoc.DocumentNode.SelectNodes("//a[@href]") ?? new HtmlNodeCollection(null))
                        {
                            string hrefValue = link.GetAttributeValue("href", string.Empty);
                            Uri resultUri;
                            if (Uri.TryCreate(new Uri(url), hrefValue, out resultUri))
                            {
                                string newUrl = resultUri.AbsoluteUri;
                                if (newUrl.StartsWith("http")) 
                                {
                                    await CrawlPage(newUrl);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    form.Invoke(new Action(() => form.ListErrors.Items.Add($"{url}: {ex.Message}")));
                }
            }
        }
    }
}
